package com.sinodata.bsm.cicp.finder.weblogic8;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sinodata.bsm.cicp.finder.Finder;

public class ExecuteQueueFinder extends Finder {

    private static final Logger logger = Logger.getLogger(ExecuteQueueFinder.class);

    /* public List find(ResValue pResValue, ResAttributeValValue[] pAttrVals, ResAttributeValue[] attributes) {
         String ip = pResValue.getIp();
         String port = pAttrVals[0].getAttrValue();
         String username = pAttrVals[1].getAttrValue();
         String password = pAttrVals[2].getAttrValue();
         String serverName = pAttrVals[3].getAttrValue();

         List resources = new ArrayList();

         String xmlString = XmlGetter.getJmxXml("WlgExecuteQueueInfo", ip, port, username, password);
         // parse xml
         try {
             Document document = DocumentHelper.parseText(xmlString);
             Element rootElement = document.getRootElement();
             Iterator<?> iterator = rootElement.elementIterator("ExecuteQueueInfo");
             while (iterator.hasNext()) {
                 Element element = (Element) iterator.next();
                 if (element != null) {
                     String svrName = element.element("ServerName").getTextTrim();
                     if (!svrName.equals(serverName))
                         continue;
                     Element nameElement = element.element("Name");
                     String name = nameElement.getTextTrim();
                     Object oneResource = CreateOneResource(serverName, name, attributes);
                     resources.add(oneResource);
                 }
             }
         } catch (DocumentException e) {
             logger.error("failed to find weblogic8 exeQueue", e);
         }
         return resources;
     }*/

    /* private Object CreateOneResource(String serverName, String name, ResAttributeValue[] attributes) {
         List oneResource = new ArrayList();
         // Resource
         ResValue res = new ResValue();
         res.setIp(host); // host IP address
         res.setName(name);// resource name
         res.setInternalId(host + "_weblogic8_" + serverName + "_ExecuteQueue" + name); // Internal
         // ID
         // for
         // agent
         res.setDescript("ExecuteQueue:" + name); // resource description
         oneResource.add(res);

         // Resource attributes
         ResAttributeValValue attribute = new ResAttributeValValue();
         attribute.setResAttributeId(attributes[0].getId());
         attribute.setAttrValue(name);
         oneResource.add(attribute);

         return oneResource;
     }*/

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        String port = (String) params.get("PORT");
        String username = (String) params.get("USERNAME");
        String password = (String) params.get("PASSWORD");
        String serverName = (String) params.get("SERVERNAME");
        String xmlString = XmlGetter.getJmxXml("WlgExecuteQueueInfo", ip, port, username, password);
        List<String[]> list = new ArrayList<String[]>();
        // parse xml
        try {
            Document document = DocumentHelper.parseText(xmlString);
            Element rootElement = document.getRootElement();
            Iterator<?> iterator = rootElement.elementIterator("ExecuteQueueInfo");
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                if (element != null) {
                    String svrName = element.element("ServerName").getTextTrim();
                    if (!svrName.equals(serverName))
                        continue;
                    Element nameElement = element.element("Name");
                    String name = nameElement.getTextTrim();
                    String[] info = new String[4];
                    list.add(info);
                    info[0] = ip + "_weblogic8_" + serverName + "_ExecuteQueue" + name;
                    info[1] = name;
                    info[2] = "ExecuteQueue:" + name;
                    info[3] = name;
                }
            }
            return list;
        } catch (DocumentException e) {
            logger.error("failed to find weblogic8 exeQueue", e);
            return new ArrayList<String[]>();
        }
    }
}